/*
 * $Id: AOItemInfo.java 27 2013-05-20 13:31:39Z antonyhands@gmail.com $
 */
package ru.olympus.antonyhands.ao.ui.info;

import java.util.List;

import ru.olympus.antonyhands.ao.data.entities.AOItem;
import ru.olympus.antonyhands.ao.data.entities.CraftInfo;
import ru.olympus.antonyhands.ao.data.entities.ItemQuantityPair;
import ru.olympus.antonyhands.ao.util.StringUtilities;

import com.vaadin.ui.Component;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

/**
 * @author AntonyHands
 * @since 15.05.2013
 */
public class AOItemInfo extends CustomComponent
{
    private static final long serialVersionUID = -6474857207848824810L;

    private AOItem _item;

    private GridLayout mainLayout;
    
    private Label _title = new Label("T");
    private Label _category = new Label("C");
    private Label _fixedPrice = new Label("FP");
    private Label _craftPrice = new Label("CP");
    private Label _marketPrice = new Label("MP");

    public AOItemInfo()
    {
        buildMainLayout();
        setCompositionRoot(mainLayout);
    }

    private GridLayout buildMainLayout()
    {
        mainLayout = new GridLayout(2, 2);
        
        mainLayout.addComponent(getMainInfoComponent(), 0, 0);

        mainLayout.addComponent(getCraftInfoComponent(), 1, 0);
        
        mainLayout.addComponent(getCraftedItemsComponent(), 0, 1);
        
        return mainLayout;
    }

    private Component getMainInfoComponent()
    {
        GridLayout mainInfoLayout = new GridLayout(3, 6);

        mainInfoLayout.addComponent(new Label("Основная информация"), 0, 0, 1, 0);

        mainInfoLayout.addComponent(new Label("Название"), 0, 1);
        mainInfoLayout.addComponent(_title, 1, 1);

        mainInfoLayout.addComponent(new Label("Категория"), 0, 2);
        mainInfoLayout.addComponent(_category, 1, 2);

        mainInfoLayout.addComponent(new Label("Фиксированная цена"), 0, 3);
        mainInfoLayout.addComponent(_fixedPrice, 1, 3);

        mainInfoLayout.addComponent(new Label("Крафтовая цена"), 0, 4);
        mainInfoLayout.addComponent(_craftPrice, 1, 4);

        mainInfoLayout.addComponent(new Label("Рыночная цена"), 0, 5);
        mainInfoLayout.addComponent(_marketPrice, 1, 5);

        return mainInfoLayout;
    }

    private void updateMainInfo()
    {
    	_title.setValue(_item.getTitle());
        _category.setValue(_item.getItemCategory().getTitle());
        _fixedPrice.setValue(StringUtilities.longToString(_item.getFixedPrice()));
        _craftPrice.setValue(StringUtilities.longToString(_item.getCraftPrice()));
        _marketPrice.setValue(StringUtilities.longToString(_item.getMarketPrice()));
    }

    private Label _craftSkill = new Label();
    private Label _craftLevel = new Label();
    private Label _workload = new Label();
    private Label _createdElementsNumber = new Label();
    private Table _resources = new Table();

    private Component getCraftInfoComponent()
    {
    	GridLayout craftInfoLayout = new GridLayout(2, 6);
    	
    	craftInfoLayout.addComponent(new Label("Производство"), 0, 0, 1, 0);
    	
    	craftInfoLayout.addComponent(new Label("Навык"), 0, 1);
    	craftInfoLayout.addComponent(_craftSkill, 1, 1);
    	
    	craftInfoLayout.addComponent(new Label("Уровень навыка"), 0, 2);
    	craftInfoLayout.addComponent(_craftLevel, 1, 2);
    	
    	craftInfoLayout.addComponent(new Label("Количество единиц труда"), 0, 3);
    	craftInfoLayout.addComponent(_workload, 1, 3);
    	
    	craftInfoLayout.addComponent(new Label("Количество произведённых элементов"), 0, 4);
    	craftInfoLayout.addComponent(_createdElementsNumber, 1, 4);
    	
    	craftInfoLayout.addComponent(new Label("Ресурсы"), 0, 5);
    	craftInfoLayout.addComponent(_resources, 1, 5);
    	
    	_resources.addContainerProperty("resTitle", String.class, null);
    	_resources.addContainerProperty("resNumber", Long.class, null);
    	_resources.setColumnHeader("resTitle", "Ресурс");
    	_resources.setColumnHeader("resNumber", "Количество");
    	_resources.setPageLength(0);
        return craftInfoLayout;
    }
    
    private void updateCraftInfo()
    {
    	if (_item.getCraftInfo() == null)
    	{
    		_craftSkill.setValue("Невозможно произвести");
    		_craftLevel.setValue("");
    		_workload.setValue("");
    		_createdElementsNumber.setValue("");
    		_resources.removeAllItems();
    	}
    	else
    	{
    		_craftSkill.setValue(_item.getCraftInfo().getCraftSkill().getTitle());
    		_craftLevel.setValue(StringUtilities.longToString(_item.getCraftInfo().getCraftLevel()));
    		_workload.setValue(StringUtilities.longToString(_item.getCraftInfo().getWorkload()));
    		_createdElementsNumber.setValue(StringUtilities.longToString(_item.getCraftInfo().getResultItemData().getValue()));
    		_resources.removeAllItems();
    		List<ItemQuantityPair> resources = _item.getCraftInfo().listInputItemsData();
    		for (int i=0; i < resources.size(); i++)
            {
                ItemQuantityPair resource = resources.get(i);
                _resources.addItem(new Object[]{
                        resource.getKey().getTitle(),
                        resource.getValue() }, i);
            }
    	}
    }

    private VerticalLayout _craftedItems = new VerticalLayout();

    private Component getCraftedItemsComponent()
    {
        return _craftedItems;
    }

    private void updateCraftedItems()
    {
        _craftedItems.removeAllComponents();
        if (_item.listCraftableItems() != null && _item.listCraftableItems().size() > 0)
        {
            _craftedItems.addComponent(new Label("<b>Изготавливаемые предметы</b>", Label.CONTENT_XHTML));
            for (CraftInfo ci : _item.listCraftableItems())
            {
                _craftedItems.addComponent(new Label(ci.getResultItem().getTitle()));
            }
        }
    }

    public void setAOItem(AOItem item)
    {
        _item = item;
        updateMainInfo();
        updateCraftInfo();
        updateCraftedItems();
        requestRepaintAll();
    }
}
